package test.blued.com.casecontroller;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import test.blued.com.casecontroller.commenthod.CommandMethod;
import test.blued.com.casecontroller.webViews.LogWebView;

/**
 * Created by BlueCity on 2017/8/22.
 */

public class HandleFile extends Activity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.handlefile);
        findView();
        init();
        //加载适配器
        spinner.setAdapter(arr_adapter);
        xmlToHtml.setOnClickListener(this);
        seeLog.setOnClickListener(this);
        identify.setOnClickListener(this);
    }
    public void findView(){
        spinner = (Spinner)findViewById(R.id.xmlName);
        xmlToHtml = (Button)findViewById(R.id.xml_to_html);
        seeLog = (Button)findViewById(R.id.seeLog);
        identify = (Button)findViewById(R.id.identify);
        language = (EditText)this.findViewById(R.id.language);
        language_re = (TextView)findViewById(R.id.lang_re);
    }
    public void init(){
        new Thread(){
            @Override
            public void run() {
                super.run();
                method.copyFile(getResources().openRawResource(R.raw.junit_noframes), "junit-noframes.xsl");
            }
        }.start();            //拷贝xsl文件

        List<String> data_list = new ArrayList<String>();
        data_list = method.getFileNames();
        arr_adapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data_list);
        //设置样式
        arr_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    }

    @Override
    public void onClick(View v) {
        final String fileName;
        switch(v.getId()){
            case R.id.xml_to_html:
                fileName = spinner.getSelectedItem().toString();
                myHandler = new MyHandler();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Message msg = myHandler.obtainMessage();
                        msg.what = 0;
                        msg.obj = fileName;
                        myHandler.sendMessage(msg);
                    }
                }).start();
                break;
            case R.id.seeLog:
                String logPath = Environment.getExternalStorageDirectory() + "/" + "QABlued/TestLog/";
                String htmlName = logPath + spinner.getSelectedItem().toString();
                Intent intent = new Intent(HandleFile.this, LogWebView.class);
                intent.putExtra("htmlName", htmlName);
                startActivity(intent);
                break;
            case R.id.identify:
//                String languageStr = language.getText().toString();
//                String language = new CommandMethod().getLanguage(languageStr);
//                language_re.setGravity(Gravity.CENTER_VERTICAL);
//                language_re.setText(language);
                Toast.makeText(HandleFile.this, "别点我，我还没被做出来!", Toast.LENGTH_LONG).show();
                break;
            default:
                Toast.makeText(HandleFile.this, "no function!", Toast.LENGTH_LONG).show();
                break;
        }
    }

    private class MyHandler extends Handler{

        public MyHandler(){}
        public MyHandler(Looper looper) { super(looper); }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    method.xmlToHtml(msg.obj.toString());
                    Toast.makeText(HandleFile.this, "转换成功！！", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    Toast.makeText(HandleFile.this, "长传成功！！", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    Toast.makeText(HandleFile.this, "handler error!", Toast.LENGTH_LONG).show();
            }
        }
    }

    private CommandMethod method = new CommandMethod();
    private Spinner spinner;
    private Button xmlToHtml;
    private Button seeLog;
    private ArrayAdapter<String> arr_adapter;
    private MyHandler myHandler;
    private Button identify;
    private EditText language;
    private TextView language_re;
}
